<template>
  <div style="margin-top: 8px; margin-left: 50px;">
    <t-form ref="formRef" :data="formData"  :rules="FORM_RULES" label-width="120px" @reset="onReset" @submit="onSubmit">
      <t-form-item label="评估对象" name="assessSubject">
        <t-radio-group v-model="formData.assessSubject">
          <t-radio :value="0">甲方</t-radio>
          <t-radio :value="1">乙方</t-radio>
        </t-radio-group>
      </t-form-item>
      <t-form-item label="项目名称" name="assessName">
      <t-input v-model="formData.assessName" placeholder="请输入项目名称" @enter="onEnter" ></t-input>
    </t-form-item>
    <t-form-item label="公司主体名称" name="company">
      <t-input v-model="formData.company" placeholder="请输入公司主体名称" @enter="onEnter" ></t-input>
    </t-form-item>
    <t-form-item label="统一信用代码" name="tyxydm">
      <t-input v-model="formData.tyxydm" placeholder="请输入统一信用代码" @enter="onEnter"></t-input>
    </t-form-item>
    <t-form-item label="评估内容" name="checked">
      <t-checkbox-group v-model="formData.checked" :options="assessmentOptions"  @change="changePG">
      </t-checkbox-group>
    </t-form-item>
    <template v-if="formData.checked.includes('0')">
      <t-form-item label="DCMM评估" name="DCMMLevel">
      <t-select v-model="formData.DCMMLevel" placeholder="请选择DCMM评估到几级" >
        <t-option label="三级" :value="0"></t-option>
      </t-select>
    </t-form-item>
    <t-form-item label="DCMM评估人" name="DCMMUserId">
      <t-select v-model="formData.DCMMUserId" placeholder="请选择DCMM评估人">
        <t-option v-for="(item,index) of DCMMList"
                        :key="index"
                        :label="item.nickName"
                        :value="item.userId"></t-option>
      </t-select>
    </t-form-item>
    <t-form-item label="计划申报时间" name="DCMMYear">
      <t-date-picker v-model="formData.DCMMYear" style="margin-right: 8px;" mode="year" clearable allow-input />
      <t-select v-model="formData.DCMMQuarter" placeholder="请选择季度/批次">
      <t-option v-for="(item,index) of jdList"
                        :key="index"
                        :label="item.label"
                        :value="item.value"></t-option> 
       </t-select>
    </t-form-item>
    <t-form-item label="报告完成时间" name="DCMMCTime">
      <t-date-picker v-model="formData.DCMMCTime" clearable allow-input />
    </t-form-item>
    </template>
    <template v-if="formData.checked.includes('1')">
      <t-form-item label="DSMM评估" name="DSMMLevel">
      <t-select v-model="formData.DSMMLevel" placeholder="请选择DSMM评估到几级">
        <t-option label="三级" :value="0"></t-option>
      </t-select>
    </t-form-item>
    <t-form-item label="DSMM评估人" name="DSMMUserId">
      <t-select v-model="formData.DSMMUserId" placeholder="请选择DSMM评估人">
        <t-option v-for="(item,index) of DSMMList"
                        :key="index"
                        :label="item.nickName"
                        :value="item.userId"></t-option>
      </t-select>
    </t-form-item>
     <t-form-item label="计划申报时间" name="DSMMQuarter">
       <t-date-picker v-model="formData.DSMMYear" style="margin-right: 8px;" mode="year" clearable allow-input />
       <t-select v-model="formData.DSMMQuarter" placeholder="请选择季度/批次">
        <t-option v-for="(item,index) of jdList"
                        :key="index"
                        :label="item.label"
                        :value="item.value"></t-option> 
       </t-select>
    </t-form-item>
    <t-form-item label="报告完成时间" name="DSMMCTime">
      <t-date-picker v-model="formData.DSMMCTime" clearable allow-input />
    </t-form-item>
    </template>
    <template v-if="formData.checked.includes('2')">
    <t-form-item label="数据合规评估" name="HGUserId">
      <t-select v-model="formData.HGUserId" placeholder="请选择评估人">
        <t-option v-for="(item,index) of HGList"
                        :key="index"
                        :label="item.nickName"
                        :value="item.userId"></t-option>
      </t-select>
    </t-form-item>
     <t-form-item label="计划申报时间" name="HGQuarter">
      <t-date-picker v-model="formData.HGYear" style="margin-right: 8px;" mode="year" clearable allow-input />
      <t-select v-model="formData.HGQuarter" placeholder="请选择季度/批次">
        <t-option v-for="(item,index) of jdList"
                        :key="index"
                        :label="item.label"
                        :value="item.value"></t-option> 
       </t-select>
    </t-form-item>
    <t-form-item label="报告完成时间" name="HGCTime">
      <t-date-picker v-model="formData.HGCTime" clearable allow-input />
    </t-form-item>
    </template>
    <t-form-item>
      <t-space :size="8">
      <t-button  type="submit" :loading="loading">确定</t-button>
      <t-button  theme="default" type="reset" variant="base">取消</t-button>
      </t-space>
    </t-form-item>
    </t-form>
  </div>
</template>
<script setup>
import { MessagePlugin } from 'tdesign-vue-next';
import { saveAssess, getPgrList } from "@/api/sjhg/assess";
import { onMounted, ref } from 'vue';
import {  omit } from 'lodash-es';
const props = defineProps({
  id: {
    type: String,
    default: null,
  },
})

const loading = ref(false)
const formRef = ref('')
const formData = ref({
  agreentmentOne: 0, //0-未签署 1-已签署
  agreentmentThree: 0,
  agreentmentTwo: 0,
  assessName: "",
  assessSubject: null,//评估对象 0-甲方 1-乙方
  company: "",
  checked:[],
  DCMMLevel:0,
  DCMMUserId:'',
  DCMMYear:null,
  DCMMQuarter:null,
  DCMMCTime:null,
  DSMMLevel:0,
  DSMMUserId:'',
  DSMMYear:null,
  DSMMQuarter:null,
  DSMMCTime:null,
  HGUserId:'',
  modelList: [],
  HGYear:null,
  HGQuarter:null,
  HGCTime:null,
  tyxydm: "" 
})
const assessmentOptions = [
  { label: 'DCMM评估', value: '0' },
  { label: 'DSMM评估', value: '1' },
  { label: '数据合规评估', value: '2' },
];
const FORM_RULES = { 
  assessSubject: [{ required: true, message: '请输入评估对象' }] ,
  assessName: [{ required: true, message: '请输入项目名称' }] ,
  company: [{ required: true, message: '请输入公司主体名称' }] ,
  tyxydm: [
    { required: true, message: '请输入统一信用代码' },
    { 
      pattern: /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/, 
      message: '请输入有效的统一社会信用代码' 
    }
  ] ,
  checked: [{ required: true, message: '请选择评估内容' }] ,
  DCMMLevel: [{ required: true, message: '请选择评估级别' }] ,
  DCMMUserId: [{ required: true, message: '请选择评估人' }] ,
  DCMMYear: [{ required: true, message: '请选择年份' }] ,
  DCMMQuarter: [{ required: true, message: '请选择季度/批次' }] ,
  DCMMCTime: [{ required: true, message: '请选择报告完成时间' }] ,
  DSMMLevel: [{ required: true, message: '请选择评估级别' }] ,
  DSMMUserId: [{ required: true, message: '请选择评估人' }] ,
  DSMMYear: [{ required: true, message: '请选择年份' }] ,
  DSMMQuarter: [{ required: true, message: '请选择季度/批次' }] ,
  DSMMCTime: [{ required: true, message: '请选择报告完成时间' }] ,
  HGUserId: [{ required: true, message: '请选择评估人' }] ,
  HGYear: [{ required: true, message: '请选择年份' }] ,
  HGQuarter: [{ required: true, message: '请选择季度/批次' }] ,
  HGCTime: [{ required: true, message: '请选择报告完成时间' }] ,
};

//季度
const jdList = [
  {value:'1',label:'第一季度/批次'},
  {value:'2',label:'第二季度/批次'},
  {value:'3',label:'第三季度/批次'},
  {value:'4',label:'第四季度/批次'}
]

//取消
const onReset = () => {
  // MessagePlugin.success('重置成功');
};
//提交
const onSubmit = ({ validateResult, firstError }) => {
  if (validateResult === true) {
    // MessagePlugin.success('提交成功');
    createAssess();
  } else {
    console.log('Validate Errors: ', firstError, validateResult);
    // MessagePlugin.warning(firstError);
  }
};
// 禁用 Input 组件，按下 Enter 键时，触发 submit 事件
const onEnter = (_, { e }) => {
  e.preventDefault();
};


//---------------------------接口---------------------------------------------
const changePG = (val)=>{
  console.log('-----',...val)
}

onMounted(() => {
  if(!props.id){
    getList(0);
    getList(1);
    getList(2);
  }
})
// 获取评估人列表
const DCMMList = ref([])
const DSMMList = ref([])
const HGList = ref([])

const getList = (code) => {
    let params = {}
    params.code = code
    getPgrList(params)
      .then((res) => {
        if (res.code == 200) {
          if(code === '0' || code === 0){
            DCMMList.value = res.data
          }else if(code === '1' || code === 1){
            DSMMList.value = res.data
          }else{
            HGList.value = res.data
          }
        } else {
        }
      })
      .finally(() => {
      });
  }

  //评估内容提交成功后 
const emit = defineEmits(["goNext"]);
//选择评估内容
const createAssess = async () => {
  loading.value = true
  try {
    console.log("this.form = ", formData.value);
    formData.value.modelList = []
    if(formData.value.checked.includes('0')){
      formData.value.modelList.push({
        acceptUserId: formData.value.DCMMUserId,
        assessLevel: formData.value.DCMMLevel,
        assessModel: 0,
        planShengBaoQuarter: formData.value.DCMMQuarter,
        planShengBaoYear: formData.value.DCMMYear,
        reportCompleteTime: formData.value.DCMMCTime
      })
    }
    if(formData.value.checked.includes('1')){
      formData.value.modelList.push({
        acceptUserId: formData.value.DSMMUserId,
        assessLevel: formData.value.DSMMLevel,
        assessModel: 1,
        planShengBaoQuarter: formData.value.DSMMQuarter,
        planShengBaoYear: formData.value.DSMMYear,
        reportCompleteTime: formData.value.DSMMCTime
      })
    }
    if(formData.value.checked.includes('2')){
      formData.value.modelList.push({
        acceptUserId: formData.value.HGUserId,
        assessModel: 2,
        planShengBaoQuarter: formData.value.HGQuarter,
        planShengBaoYear: formData.value.HGYear,
        reportCompleteTime: formData.value.HGCTime
      })
    }
    const submitData = omit(formData.value, ['checked', 'DCMMLevel','DCMMUserId','DCMMQuarter','DCMMYear','DCMMCTime'
      ,'DSMMLevel','DSMMUserId','DSMMQuarter','DSMMYear','DSMMCTime','HGUserId','HGQuarter','HGYear','HGCTime'
    ]);
    console.log("submitData = ", submitData);
    saveAssess(submitData)
      .then((res) => {
        if (res.code == 200) {
          MessagePlugin.success('成功');
          //成功后跳转到下一步
          emit('goNext',res.data)
        } else {
          MessagePlugin.error(res.msg);
        }
      })
      .finally(() => {
        loading.value = false
      });
  } catch (error) {
    loading.value = false
    console.error("验证失败:", error);
  }
};


</script>
<style lang="scss" scoped>
::deep(.t-date-picker, .t-date-range-picker ){
  width: 100%;
}
// .el-form-item__content .el-radio-group  {
//   display: flex;
//   align-items: flex-start;
// }
// .el-form-item {
//   margin-bottom: 24px;
// }

.btn{
  display: flex;
  align-items: flex-start;
  margin-left: 120px;
  margin-top: 8px;
}
</style>
